package org.apogee.mil.client.widgets;

import java.util.ArrayList;

import org.apogee.mil.client.factories.ServerServiceFactory;
import org.apogee.mil.client.utils.Session;
import org.apogee.mil.shared.Constants.DisplayFormat;
import org.apogee.mil.shared.Constants.MessagesField;
import org.apogee.mil.shared.User;

import com.extjs.gxt.ui.client.core.XTemplate;
import com.extjs.gxt.ui.client.data.BaseModel;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.RowExpander;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;

public class MessageDisplayWidget implements IsWidget {

	private ListStore<BaseModel> messages = new ListStore<BaseModel>();
	private Grid<BaseModel> grid;
	private int parentHeight;
	private int parentWidth;
	
	public MessageDisplayWidget(int width, int height) {
		parentHeight = height;
		parentWidth = width;
	}

	@Override
	public Widget asWidget() {
		ContentPanel cp = new ContentPanel();
		cp.setBorders(true);
		cp.setHeading("View Messages");
		cp.setSize(parentWidth - 10, parentHeight - 50);
		cp.setAnimCollapse(true);  
	    cp.setCollapsible(true);  
	    cp.setLayout(new FitLayout());
		
		ArrayList<ColumnConfig> configs = new ArrayList<ColumnConfig>();
		
		XTemplate tpl = XTemplate.create("<p><b><u>Message</u></b><br/><span style='white-space:normal;'>{message}</span></p>");
		
		RowExpander expander = new RowExpander();
		expander.setTemplate(tpl);
		
		configs.add(expander);
		
		ColumnConfig column = new ColumnConfig();
		column.setId(DisplayFormat.USER_FMT);
		column.setHeader("From");
		column.setWidth(115);
		configs.add(column);
		
		column = new ColumnConfig();
		column.setId(MessagesField.DATE);
		column.setHeader("Date");
		column.setWidth(115);
		configs.add(column);
		
		ColumnModel cm = new ColumnModel(configs);
		
		grid = new Grid<BaseModel>(messages, cm);
		grid.addPlugin(expander);
		grid.setColumnReordering(true);
		grid.getView().setAutoFill(true);
		grid.setSize(parentWidth - 10, parentHeight - 50);
		
		cp.add(grid);
		
		
		ToolBar tb = new ToolBar();
		Button mail = new Button("Check Messages");
		mail.addSelectionListener(new SelectionListener<ButtonEvent>() {
			
			@Override
			public void componentSelected(ButtonEvent ce) {
				if (grid.isMasked() == false) {
					update(ServerServiceFactory.getUser(Session.getSessionId()));
				}
				
			}
		});
		
		tb.add(mail);
		cp.setBottomComponent(tb);
		
		grid.mask("Login Frst\nTo See Your Messages");
		
		return cp;
	}

	public void update(User currentUser) {
		grid.unmask();
		ServerServiceFactory.getGreetService(currentUser.getSessionId()).getMessages(currentUser.getUserId(), new AsyncCallback<ArrayList<BaseModel>>() {
			
			@Override
			public void onSuccess(ArrayList<BaseModel> result) {
				GWT.log("Result size: " + result.size());
				if (result.size() == 0) {
					return;
				}
				
				grid.getStore().removeAll();
				grid.getStore().add(result);
				grid.getView().refresh(true);
				
			}
			
			@Override
			public void onFailure(Throwable caught) {
				MessageBox.alert("Error", "Unable to get messages\n" + caught.getMessage(), null);
				
			}
		});
		
	}

	public void clearMessages() {
		grid.mask("Login Frst\nTo See Your Messages");
		grid.getStore().removeAll();
		grid.getView().refresh(true);
		
	}

}
